package Servlet;

import Bean.User;
import Service.AuditService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-12
 * Time: 21:29
 */
//机票改签
@WebServlet("/AmendTicketServlet")
public class AmendTicketServlet extends HttpServlet {
    private AuditService auditService = new AuditService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf8");
        //捕获数据
        String username = req.getParameter("jptg-name");
        String idNumber = req.getParameter("idNumber");
        String originalFlight = req.getParameter("originalFlight");
        String newFlight = req.getParameter("newFlight");
        String gqSeatNumber = req.getParameter("seatNumber");


        User user = (User) req.getSession().getAttribute("LoginUser");

        if(!user.getIdentityCard().equals(idNumber) || !user.getName().equals(username)) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "用户信息错误!!!");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }


        int ret = auditService.change(user.getUserId(),originalFlight,newFlight,gqSeatNumber);

        if (ret == 2) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "用户并没有购买航班为 : " + originalFlight + "的机票");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        } else if(ret == 3) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "航班号为 : " + newFlight +"的航班存在");
            req.setAttribute("redirectUrl", "user_index.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }

        //改签成功就提示一下
        req.setAttribute("status", "success");
        req.setAttribute("message", "提交申请成功!!");
        req.setAttribute("redirectUrl", "user_index.jsp");
        req.setAttribute("waitSeconds", 3);
        req.getRequestDispatcher("skip.jsp").forward(req,resp);
    }
}
